package com.example.springboot;

import com.example.springboot.util.SearchUtil;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.Version;
import org.junit.Before;
import org.junit.Test;

/**
 * Created by tangmayao on 2018/4/4.
 */
public class TestSearchUtil {

    SearchUtil searchUtil ;

    @Before
    public  void before(){
         searchUtil=SearchUtil.getInstance();
        searchUtil.initSearchUtil();
    }

    @Test
     public void testQueryTerm(){

        searchUtil.searchByTerm("content","hello",10);
     }


     @Test
     public  void testQueryTermRange(){
         searchUtil.searchByTermRange("content","a","s",10);
     }

     @Test
     public  void testQueryParser(){
         QueryParser queryParser=new QueryParser(Version.LUCENE_CURRENT,"content",new StandardAnalyzer(Version.LUCENE_CURRENT));
         //默认空格 是Operator.OR
//         queryParser.setDefaultOperator(QueryParser.Operator.AND);
         Query query;
         try {
             //搜索content 中有like的
             //query  =queryParser.parse("like");

             //搜索content 中有football 或者basketball的  空格表示或
            // query=queryParser.parse("basketball football");

             //改变搜索域为name
            // query=queryParser.parse("name:tangmayao");


             //可以使用*或者?通配符来进行查询
             //query=queryParser.parse("name:tang*");

             //匹配name没有mike 但是content中必须要有like
            // query=queryParser.parse("-name:mike+content+like"); ×

             //匹配一个区间
            // query=queryParser.parse("id:[1 TO 6]"); ×

             //完全匹配,不介意大小写
             //query=queryParser.parse("\"i like football\"");

             //匹配i 与basketball之间有一个距离
             //query=queryParser.parse("\"i basketball\" ~1");

             //模糊查询
             //query=queryParser.parse("name:make~");

             //没有办法匹配数字范围
             query=queryParser.parse("attach:[2 TO 10]");

             searchUtil.searchByQueryParser(query, 10);
         } catch (ParseException e) {
             e.printStackTrace();
         }

     }

     @Test
     public  void testSearchAfter(){
         QueryParser queryParser=new QueryParser(Version.LUCENE_CURRENT,"content",new StandardAnalyzer(Version.LUCENE_CURRENT));
         try {
             Query query  =queryParser.parse("like");
             searchUtil.searchAfter(2,2,query);
         } catch (ParseException e) {
             e.printStackTrace();
         }


     }
}